<template>
  <div>
    <!-- 面包屑 -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item><a href="/">活动管理</a></el-breadcrumb-item>
      <el-breadcrumb-item>活动列表</el-breadcrumb-item>
      <el-breadcrumb-item>活动详情</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 表格 -->
    <div class="zhuyao">
      <el-button type="primary" @click="dialogFormVisible = true"
        >添加用户</el-button
      >
    </div>
    <template>
      <el-table :data="tableData">
        <el-table-column type="expand">
          <template slot-scope="scope">
            <el-row v-for="item in scope.row.children" :key="item.id">
              <el-col :span="5">
                <el-tag @close="removeroles(scope.row, item.id)" closable>
                  {{ item.authName }}
                </el-tag>
                <i class="el-icon-caret-right"></i>
              </el-col>
              <el-col :span="19">
                <el-row v-for="item1 in item.children" :key="item1.id">
                  <el-col :span="6">
                    <el-tag
                      @close="removeroles(scope.row, item1.id)"
                      closable
                      type="success"
                    >
                      {{ item1.authName }}
                    </el-tag>
                    <i class="el-icon-caret-right"></i>
                  </el-col>
                  <el-col :span="18">
                    <el-tag
                      @close="removeroles(scope.row, item2.id)"
                      closable
                      type="warning"
                      v-for="item2 in item1.children"
                      :key="item2.id"
                    >
                      {{ item2.authName }}
                    </el-tag>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-table-column>
        <el-table-column label="#" type="index"> </el-table-column>
        <el-table-column label="角色名称" prop="roleName"> </el-table-column>
        <el-table-column label="角色描述" prop="roleDesc"> </el-table-column>
        <el-table-column label="操作">
          <template class="annui" slot-scope="scope">
            <el-button
              type="primary"
              icon="el-icon-edit"
              circle
              @click="onedit(scope.row)"
              >编辑</el-button
            >
            <el-button
              type="danger"
              icon="el-icon-delete"
              circle
              @click="danger(scope.row)"
              >删除</el-button
            >
            <el-button
              type="warning"
              icon="el-icon-setting"
              circle
              @click="fenpei(scope.row)"
              >分配权限</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </template>
    <!--  -->
    <el-dialog title="添加角色" :visible.sync="dialogFormVisible">
      <el-form :model="form">
        <el-form-item label="角色名称">
          <el-input v-model="form.roleName"></el-input>
        </el-form-item>
        <el-form-item label="角色描述">
          <el-input v-model="form.roleDesc"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="AddFormVisible">确 定</el-button>
      </div>
    </el-dialog>
    <!-- 修改弹出框 -->
    <!-- 修改弹出框 -->
    <el-dialog title="修改用户" :visible.sync="dialogFormVisible1">
      <el-form :model="form">
        <el-form-item label="角色名称">
          <el-input v-model="form.roleName" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="角色描述">
          <el-input v-model="form.roleDesc" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible1 = false">取 消</el-button>
        <el-button type="primary" @click="que">确 定</el-button>
      </div>
    </el-dialog>
    <!-- 分配权限弹出框 -->
    <el-dialog
      title="提示"
      :visible.sync="dialogVisible"
      width="30%"
      @close="setrightdialog"
    >
      <el-tree
        :data="jurisdiction"
        show-checkbox
        node-key="id"
        :default-expanded-keys="[2, 3]"
        :default-checked-keys="init"
        highlight-current
        :props="defaultProps"
        ref="treeref"
      >
      </el-tree>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="edutr">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      tableData: [],
      ser: "",
      gridData: [],
      dialogTableVisible: false,
      dialogFormVisible: false,
      dialogFormVisible1: false,
      // dialogVisible: false,
      dialogVisible: false,
      form: {
        roleName: "",
        roleDesc: "",
      },
      jurisdiction: [],
      checkarr: [],
      id: "",
      formLabelWidth: "120px",
      defaultProps: {
        children: "children",
        label: "authName",
      },
      roleid: 0,
      rowID:'',
      init:[]
    };
  },
  methods: {
    //删除
    danger(item) {
      console.log(this.tableData);

      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          // console.log(item);
          this.$http.delete(`roles/${item.id}`).then((res) => {
            // console.log(res);

            this.$http.get("/roles").then((res) => {
              console.log(res);

              console.log(this.tableData);
              this.getroles();
            });
            this.$message({
              type: "success",
              message: res.meta.msg,
            });
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    //封装的请求的数据
    getroles() {
      this.$http.get("/roles").then((res) => {
        console.log(res);
        this.tableData = res.data;
      });
    },
    //添加按钮
    AddFormVisible() {
      console.log(this.form.roleName);
      this.$http({
        url: "/roles",
        method: "post",
        data: {
          roleDesc: this.form.roleDesc,
          roleName: this.form.roleName,
        },
      }).then((res) => {
        console.log(res);
        if (res.meta.status != 201) {
          this.$message({
            type: "errot",
            message: res.meta.msg,
          });
        } else {
          this.$message({
            type: "success",
            message: res.meta.msg,
          });
          this.form = {
            roleName: "",
            roleDesc: "",
          };
          this.dialogFormVisible = false;
          this.getroles();
        }
      });
    },
    //编辑数据
    onedit(item) {
      console.log(item);
      this.dialogFormVisible1 = true;
      this.id = item.id;
      this.$http({
        url: `/roles/${item.id}`,
        method: "get",
      }).then((res) => {
        console.log(res);
        (this.form.roleName = res.data.roleName),
          (this.form.roleDesc = res.data.roleDesc);
      });
    },
    que() {
      this.$http
        .put(`roles/${this.id}`, {
          roleName: this.form.roleName,
          roleDesc: this.form.roleDesc,
        })
        .then((res) => {
          console.log(res);
          this.getroles();
          this.dialogFormVisible1 = false;
        });
    },
    //分配权限
    fenpei(item) {
      this.roleid = item.id;
       this.init = []
      console.log(item);
      this.dialogVisible = true;
      this.$http.get("rights/tree").then((res) => {
        console.log(res);
        this.jurisdiction = res.data;
        console.log(res.data);
        this.getlimits(item,this.init)
      });
    },
    getlimits(node,arr){
      if(!node.children){
        return arr.push(node.id)
      }
      node.children.forEach(item=>this.getlimits(item,arr))
    },
    edutr() {
      var b = [
        ...this.$refs.treeref.getCheckedKeys(),
        ...this.$refs.treeref.getHalfCheckedKeys(),
      ];
      console.log(b);
      var a = b.join(",");
      console.log(a);
      console.log(this.$refs.treeref);
      this.$http
        .post(`/roles/${ this.roleid} /rights`, {
          rids: a,
        })
        .then((res) => {
          console.log(res);
          this.dialogVisible = false;
          this.getroles();
        });
        
    },
    setrightdialog(){
      this.init = []
    }
  },

  created() {
    this.getroles();
  },
};
</script>

<style>
.demo-table-expand {
  font-size: 0;
}
.demo-table-expand label {
  width: 90px;
  color: #99a9bf;
}
.demo-table-expand .el-form-item {
  margin-right: 0;
  margin-bottom: 0;
  width: 50%;
}
.el-breadcrumb {
  font-size: 14px;
  line-height: 1;
  margin-bottom: 20px;
}
.el-main {
  background-color: #e9eef3;
  color: #333;
  text-align: center;
  line-height: 0px;
}
.zhuyao {
  width: 100%;
  height: 40px;
  text-align: left;
}
</style>
